package com.cocostudio.erpapp.view;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.webkit.WebView;

public class ObservableWebView extends WebView {
    private OnScrollChangedCallback mOnScrollChangedCallback;
    private float downx;
    private float downy;
    private long downTime;
    private OnTapListener tapListener;
    private NavigateView navigateView;
    private boolean inTouchZoomMode;

    public ObservableWebView(final Context context) {
        super(context);
        init();
    }

    public ObservableWebView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ObservableWebView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public void setOnTapListener(OnTapListener listener) {
        tapListener = listener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            downx = e.getX();
            downy = e.getY();
            downTime = SystemClock.currentThreadTimeMillis();
            break;
        case MotionEvent.ACTION_UP:
            int slop = ViewConfiguration.getTouchSlop();
            long timeout = ViewConfiguration.getTapTimeout();
            if (Math.abs(e.getX() - downx) < slop * 2 && Math.abs(e.getY() - downy) < slop * 2 && (SystemClock.currentThreadTimeMillis() - downTime) < timeout) {
                int x = getScrollX() + (int) e.getX();
                int y = getScrollY() + (int) e.getY();
                moveFocusPointTo(x, y);
                float r = 1f / getScale();
                if (tapListener != null) {
                    tapListener.onTap((int) (x * r), (int) (y * r));
                }
                return false;
            }
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            inTouchZoomMode = true;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            inTouchZoomMode = false;
            break;
        case MotionEvent.ACTION_MOVE:
            if (inTouchZoomMode) {
                if (navigateView != null) {
                    navigateView.zoom(1f / getScale());
                }
            }
            break;
        }
        return super.onTouchEvent(e);
    }

    private void init() {
    }

    public void setNavigateView(NavigateView view) {
        navigateView = view;
    }

    public NavigateView getNavigateView() {
        return navigateView;
    }

    public void moveFocusPointTo(float x, float y) {
        loadUrl("javascript:var _div=document.getElementById('div');_div.style.top=" + (y / getScale() - 18) + ";_div.style.left=" + (x / getScale() - 18) + ";");
    }

    @Override
    protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedCallback != null)
            mOnScrollChangedCallback.onScroll(l, t, oldl, oldt);
    }

    public OnScrollChangedCallback getOnScrollChangedCallback() {
        return mOnScrollChangedCallback;
    }

    public void setOnScrollChangedCallback(final OnScrollChangedCallback onScrollChangedCallback) {
        mOnScrollChangedCallback = onScrollChangedCallback;
    }

    /**
     * Impliment in the activity/fragment/view that you want to listen to the
     * webview
     */
    public static interface OnScrollChangedCallback {
        public void onScroll(int l, int t, int oldl, int oldt);
    }

    public static interface OnTapListener {
        public void onTap(int x, int y);
    }
}
